<script setup lang="ts">
import {ref} from 'vue'
import {cn} from "@/libs/protobuf/js/plaza";
import Request = cn.owng.protobuf.plaza.Request;
import Response = cn.owng.protobuf.plaza.Response;

const form = ref({
  command: '{"login": {"username": "your_username", "password": "your_password"}}',
  response: ''
})

const socket = new WebSocket('ws://localhost:8081')
socket.binaryType = 'arraybuffer'

socket.onmessage = (event) => {
  const buffer = new Uint8Array(event.data)
  form.value.response = JSON.stringify(Response.decode(buffer).toJSON())
}

const onSubmit = () => {
  console.log('onSubmit', form.value.command)
  const buffer = Request.encode(Request.create(JSON.parse(form.value.command))).finish()
  socket.send(buffer)
}

</script>
<template>
  <div class="test">
    <el-card>
      <template #header>
        <div class="card-header">
          <h3>协议测试</h3>
        </div>
      </template>
      <el-form :model="form" label-width="auto">
        <el-form-item label="请求">
          <el-input v-model="form.command" type="textarea" :rows="3"/>
        </el-form-item>
        <el-form-item label="响应">
          <el-input type="textarea" :rows="10" v-model="form.response"></el-input>
        </el-form-item>
        <div class="align-right">
          <el-button type="primary" @click="onSubmit">发送</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<style>
</style>
